import { EmitsOption, EmptyEnum } from "@/view/compont/crud/component/crudManager/type.d";
import type { UserModel } from "./model";
import { SexEnum } from "./model.d";
import { getUserList } from "@/server/user/api";
import { userFormOption } from "./form";

export default class useUserApi {
  constructor() {
    console.log("useUserApi");
    userFormOption.sex.option = [
      { label: "男性", value: SexEnum.male },
      { label: "女性", value: SexEnum.female },
    ];
  }

  /**
   * 更新数据的方法
   * @param data
   * @param closeDialog
   * @param onError
   */
  public updateData: EmitsOption<UserModel>["updateData"] = (
    data,
    closeDialog,
    onError
  ) => {
    getUserList(data.id)
      .then(closeDialog)
      .catch((err) => {
        console.log(err);
        onError(err.message);
      });
  };

  public getUserList: EmitsOption<UserModel>["getList"] = (onSuccess, onError) => {
    setTimeout(() => {
    //   const list: Array<UserModel> = Array.from({ length: 100 }).map(
    //     (_, id) => {
    //       return {
    //         id: id,
    //         name: "张三" + id,
    //         age: 24,
    //         sex: 0,
    //         address:
    //           "中华人名共和国湖南省长沙市雨花区砂子塘3栋1单元303室，中华人名共和国湖南省长沙市雨花区砂子塘3栋1单元303室，中华人名共和国湖南省长沙市雨花区砂子塘3栋1单元303室",
    //       };
    //     }
    //   )

    //   onSuccess(list, 100)
    // onSuccess([], 0)
    onError(EmptyEnum[200])
    }, 2000);
  };
}
